package com.yanqu.road.server.cmd.activity.operamask;

import com.yanqu.road.logic.activity.operamask.OperaMaskSceneRankListModel;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.OperaMaskProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.operamask.CrossOperaMaskMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = CrossProtocol.C2_OPERA_MASK_GAME_OVER_CHANGE_RANK, desc = "")
public class CrossOperaMaskGameOverCmd extends CrossCommand {

    private OperaMaskProto.CrossOperaMaskGameOverMsg reqMsg;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = OperaMaskProto.CrossOperaMaskGameOverMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        int activityId = reqMsg.getActivityId();
        int type = reqMsg.getType();
        int sceneId = reqMsg.getSceneId();
        long userId = packet.getPlayerId();

        OperaMaskSceneRankListModel model = CrossOperaMaskMgr.getOperaMaskSceneRankListModel(activityId, 0, sceneId);
        int newRank = model.getMyRank(userId);

        // 移动方块结束游戏
        if (type == 1) {
            OperaMaskProto.OperaMaskMoveBlockRespMsg reqMove = reqMsg.getReqMove();
            OperaMaskProto.OperaMaskMoveBlockRespMsg.Builder builder = reqMove.toBuilder();
            OperaMaskProto.OperaMaskResultMsg.Builder builder1 = builder.getResult().toBuilder();
            OperaMaskProto.OperaMaskSceneEndMsg.Builder builder2 = builder.getResult().getSceneEnd().toBuilder();
            builder2.setOldRank(builder2.getOldRank());
            builder2.setNewRank(newRank);
            builder1.setSceneEnd(builder2);
            builder.setResult(builder1);
            YanQuMessage message = YanQuMessageUtils.buildMessage(ClientProtocol.U_OPERA_MASK_MOVE_BLOCK, builder);
            MessageHelper.sendPacket(serverId, userId, message);
            return;
        }

        // 取消关卡结束游戏
        if (type == 2) {
            OperaMaskProto.OperaMaskCancelSceneRespMsg reqCancel = reqMsg.getReqCancel();
            OperaMaskProto.OperaMaskCancelSceneRespMsg.Builder builder = reqCancel.toBuilder();
            OperaMaskProto.OperaMaskSceneEndMsg.Builder builder1 = builder.getSceneEnd().toBuilder();
            builder1.setOldRank(builder1.getOldRank());
            builder1.setNewRank(newRank);
            builder.setSceneEnd(builder1);
            YanQuMessage message = YanQuMessageUtils.buildMessage(ClientProtocol.U_OPERA_MASK_CANCEL_SCENE, builder);
            MessageHelper.sendPacket(serverId, userId, message);
            return;
        }

    }

    @Override
    public void sendData() {

    }
}
